package com.nail.api.customMake.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.nail.utils.PageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.nail.api.customMake.bean.Template;
import com.nail.api.customMake.service.ITemplateService;
import com.nail.api.user.bean.User;
import com.nail.client.RedisClient;
import com.nail.common.Constant;
import com.nail.utils.IPUtils;
import com.nail.utils.Result4API;
import com.nail.controller.BaseController;

@Controller
@RequestMapping("/api/template")
public class TemplateController  extends BaseController{
    Logger logger = Logger.getLogger(this.getClass());

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private ITemplateService templateService;

    @PostMapping(value="/addTemplate")
    @ResponseBody
    public Result4API addTemplate(HttpServletRequest request, Template template,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：addTemplate" );

        if(template == null || template.getOriginalPrice() == null || template.getPrice() == null || template.getName() == null
        		|| template.getTemplateType() == null || template.getIsAble() == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
        template.setUserId(user.getId());
        template.setUserName(user.getUserName());
        template.setCreateTime(new Date());

        if(template.getImgIds() != null){
            template.setImgIds(template.getImgIds().replaceAll("\\[","").replaceAll("\\]","").replaceAll("\"",""));
        }

        try {
        	templateService.addTemplate(template);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }

    @PostMapping(value="/deleteTemplate")
    @ResponseBody
    public Result4API deleteTemplate(HttpServletRequest request, Long templateId, String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：deleteTemplate" );

        if(templateId == null ){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        try {
        	templateService.deleteTemplate(templateId);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }

    @PostMapping(value="/updateTemplate")
    @ResponseBody
    public Result4API updateTemplate(HttpServletRequest request,Template template,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：updateTemplate" );

        if(template ==null || template.getId() == null ){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
        template.setModifier(user.getId());
        template.setUpdateTime(new Date());
        if(template.getImgIds() != null){
            template.setImgIds(template.getImgIds().replaceAll("\\[","").replaceAll("\\]","").replaceAll("\"",""));
        }
        try {
        	templateService.updateTemplate(template);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/getTemplate")
    @ResponseBody
    public Result4API getTemplate(HttpServletRequest request,String token,String templateType,Integer pageSize,Integer pageNo){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getTemplate" );

        if(StringUtils.isEmpty(token)){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }
        PageResult pageResult = new PageResult();
        if(pageNo != null){
            pageResult.setPageNo(pageNo);
        }
        if(pageSize != null){
            pageResult.setPageSize(pageSize);
        }
        try {
            //templateType 模板类型，0-指甲，1-手机壳，没有则全查
            pageResult = templateService.getTemplate(pageResult,templateType);
            result.setData(pageResult);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/getTemplateById")
    @ResponseBody
    public Result4API getTemplateById(HttpServletRequest request,String token,Long id){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：getTemplateById" );

        if(StringUtils.isEmpty(token)){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        try {
            User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);
            Template template = templateService.getTemplateById(user,id);
            result.setData(template);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }



    @PostMapping(value="/setSort")
    @ResponseBody
    public Result4API setSort(HttpServletRequest request,Long id,Integer sort,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：setSort" );

        if(sort ==null || id == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        try {
            templateService.setSort(id,sort);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


    @PostMapping(value="/setIsAble")
    @ResponseBody
    public Result4API setIsAble(HttpServletRequest request,Long id,Integer isAble,String token){
        Result4API result = new Result4API();
        logger.info(sdf.format(new Date()) +" "+ IPUtils.getRemoteHost(request) + ",用户访问了本接口：setIsAble" );

        if(isAble ==null || id == null){
            result.setCode(Constant.API_CODE_501);
            result.setMsg(Constant.API_MSG_501);
            return result;
        }

        User user = (User)redisClient.get(Constant.REDIS_USER_KEY + token);

        //无权访问
        if(user.getRole() == null || !user.getRole().getId().equals(1L)){
            result.setCode(Constant.API_CODE_1003);
            result.setMsg(Constant.API_MSG_1003);
            return result;
        }

        try {
            templateService.setIsAble(id,isAble);
            result.setCode(200);
            result.setMsg(Constant.API_MSG_SUCCESS);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
            result.setCode(Constant.API_CODE_ERROR);
            result.setMsg(Constant.API_MSG_ERROR);
            return result;
        }
    }


}
